package com.weijian.controller;

import com.weijian.common.result.ApiResponse;
import com.weijian.dal.entity.Shipment;
import com.weijian.service.ShipmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货物管理接口
 */
@Tag(name = "货物管理", description = "货物增删改查接口")
@RestController
@RequestMapping("/api/shipments")
public class ShipmentController {

    @Autowired
    private ShipmentService shipmentService;

    /**
     * 根据dataId查询货物
     *
     * @param dataId 数据ID
     * @return 货物列表
     */
    @Operation(summary = "根据dataId查询货物")
    @GetMapping("/by-data")
    public ApiResponse<List<Shipment>> getShipmentsByDataId(
            @Parameter(description = "数据ID") @RequestParam Integer dataId) {
        List<Shipment> shipments = shipmentService.getShipmentsByDataId(dataId);
        return ApiResponse.success(shipments);
    }

    /**
     * 根据起点和终点查询货物
     *
     * @param originNode 起点
     * @param destinationNode 终点
     * @return 货物列表
     */
    @Operation(summary = "根据起点和终点查询货物")
    @GetMapping("/search")
    public ApiResponse<List<Shipment>> searchShipments(
            @Parameter(description = "起点") @RequestParam(required = false) Integer originNode,
            @Parameter(description = "终点") @RequestParam(required = false) Integer destinationNode) {
        List<Shipment> shipments = shipmentService.searchShipments(originNode, destinationNode);
        return ApiResponse.success(shipments);
    }

    /**
     * 根据ID查询货物详情
     *
     * @param id 货物ID
     * @return 货物详情
     */
    @Operation(summary = "根据ID查询货物详情")
    @GetMapping("/{id}")
    public ApiResponse<Shipment> getShipmentById(
            @Parameter(description = "货物ID") @PathVariable Integer id) {
        Shipment shipment = shipmentService.getById(id);
        if (shipment == null) {
            return ApiResponse.notFound("货物不存在");
        }
        return ApiResponse.success(shipment);
    }
}